<?php
function input_csv($filename, $charset = 'utf-8') { 
    $handle = fopen($filename, 'r');
    $out = array (); 
    $n = 0; 
    while ($data = fgetcsv($handle, 10000)) { 
        $num = count($data); 
        for ($i = 0; $i < $num; $i++) {
            if($charset == 'utf-8'){
                $out[$n][$i] = iconv('gb2312','utf-8//IGNORE',$data[$i]);
            }else{
                $out[$n][$i] = $data[$i]; 
            }            
        } 
        $n++; 
    } 
    return $out; 
}
/**
 *   $header 是一维数组
 *   $data 是二维数组
 * @example export_csv('abc.csv',array('name'=>'姓名','mobile'=>'电话'), array(array('name'=>'苏啊','mobile'=>'1850000')));
 */ 
function export_csv($filename,$header= array(), $data = array()) {
    header("Content-type:text/csv"); 
    header("Content-Disposition:attachment;filename=".$filename); 
    header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
    header('Expires:0'); 
    header('Pragma:public'); 
    
    $str = join(",", $header)."\n";
    $str = iconv('utf-8','gb2312//IGNORE',$str); 
    $keys = array_keys($header);
    foreach($data as $value){        
        if(is_array($value)){
            $s = array();
            foreach($keys as $v){
                 $s[] = iconv('utf-8','gb2312//IGNORE',$value[$v]);
            }
            $s = join(",", $s)."\n";
            $str .= $s;
        }
    }
    
    echo  $str; exit;
} 


$r = input_csv('test.csv');
print_r($r);
    
    
    //$filename = date('Ymd').'.csv'; //设置文件名 
    //export_csv($filename,array('name'=>'姓名','mobile'=>'电话'), array(array('name'=>'苏啊','mobile'=>'1850000'),array('name'=>'苏2','mobile'=>'3331850000'))); //导出 